<?php

namespace app\api\controllers;

use \Cm;
use \Yii;
use app\api\core\BaseController;
use app\api\interfaces\WxInterface;
use app\api\models\WxModel;

class WxController extends BaseController implements WxInterface
{
	public function actionAuth() {

		$cid = Cm::$req->post('company_id', ['title'=>'公司id', 'null', 'empty']);

		$compnay = file_get_contents("php://input");

		$compnayArr = json_decode($compnay, true);
		if (isset($compnayArr['company_id'])) {
		    $cid = $compnayArr['company_id'];
        }
		$options = [
			'component_appid' => '',
			'component_appsecret' => '',
			'component_verify_ticket' => '',
		];
		//获取ticket
		$appId = Yii::$app->params['wechat']['appid'];
		$ticket = Cm::$db->one("SELECT component_verify_ticket,appsecret FROM `scrm_weixin_three_account` WHERE appid='{$appId}'");
		if (!$ticket) {
			Cm::$res->error('未配置微信第三方平台');
		}

		$options['component_appid'] = $appId;
		$options['component_appsecret'] = $ticket['appsecret'];
		$options['component_verify_ticket'] = $ticket['component_verify_ticket'];
		$auth = new \Auth($options);

		$code = $auth->get_auth_code(); #获取预授码
		$url = $auth->getRedirect(Yii::$app->params['auth_back_url'] . "?cid={$cid}", $code);

		Cm::$res->susess('success', [
			'appid' => $options['component_appid'],
			'url' => $url,
		]);
	}

	/**
	 * 获取微信已授权账号：
	 */
	function actionAuthorizer_info()
    {
        return (new WxModel())->actionAuthorizerInfo();
	}

    public function actionGet_app_id()
    {
        return (new WxModel())->actionGetAppId();
    }

    public function actionSet_account_info()
    {
        return (new WxModel())->actionSetAccountInfo();
    }
}